package com.huitone.smspfm.inputs.sharem;

import java.io.FileInputStream;
import java.io.IOException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Configurator;

import com.bin.common.utils.StringUtils;

/**   
* @Title Main.java 
* @Package com.huitone.smspfm.inputs.sharem 
* @Description TODO(用一句话描述该文件做什么) 
* @author ybliang   
* @date 2017年10月16日 上午12:11:06 
* @version V1.0   
*/
public class Main {
	
	static {
		try {
			Configurator.initialize(null, new ConfigurationSource(new FileInputStream(System.getProperty("log4j2.xml"))));
		} catch (Exception e) {
			System.out.println("初始化log4j2配置文件错误");
		}
	}
	
	private static final Logger logger = LogManager.getLogger(Main.class);
	
	public static void main(String[] args) {
		
		String fileName = getFileName();
		
		ShareMemory memory = null;
		
		try {
			memory = new ShareMemory(fileName, 10240);
		} catch (IOException e) {
			logger.error("创建ShareMemory对象时报错", e);
		}
		
		if (memory != null) {
			
			if (args != null) {
				
				StringBuffer commands = new StringBuffer();
				
				for (int i = 0; i < args.length ; i ++) {
					commands.append(args[i]);
					commands.append(" ");
				}
				
				try {
					String string = StringUtils.trim(commands.toString());
					logger.info("ShareMemory外部输入信号:[" + string + "]");
					memory.write(string);
				} catch (IOException e) {
					logger.error("ShareMemory外部信号时报错", e);
				}
			}
		}
	}
	
	private static String getFileName() {
		
		String filename = System.getProperty("ShareMemoryFile");
		if (StringUtils.isEmpty(filename)) {
			filename = System.getenv("ShareMemoryFile");
		}
		
		return filename;
	}

}
